package com.situ.hotel.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.hotel.dao.DeptDao;
import com.situ.hotel.dao.EmpDao;
import com.situ.hotel.entity.Dept;
import com.situ.hotel.entity.Emp;
import com.situ.hotel.util.JDBCUtil;
import com.situ.hotel.util.JSONResult;
import com.situ.hotel.util.JSONUtil;

// 继承HttpServlet，Servlet和Jsp都是可以在浏览器直接访问
// http://localhost:8080/Hotel/emp
@WebServlet("/emp")
public class EmpServlet extends HttpServlet{
	private EmpDao empDao = new EmpDao();
	private DeptDao deptDao = new DeptDao();

	// 访问Servlet默认就是执行Service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 结局post请求乱码问题
		req.setCharacterEncoding("UTF-8");
		// syst
		System.out.println("EmpServlet.service()");
		//  http://localhost:8080/Hotel/emp?method=selectAll
		//  http://localhost:8080/Hotel/emp?method=delete&code=001
		String method = req.getParameter("method");
		// method没有值，默认让他查找所有
		if (method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "getAddPage":
			getAddPage(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
//		case "getUpdatePage":
//			getUpdatePage(req, resp);
//			break;
//		case "update":
//			update(req, resp);
//			break;

		default:
			break;
		}
		
	}

	

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		String address = req.getParameter("address");
		String birthday = req.getParameter("birthday");// "2020-11-11"
		String deptCode = req.getParameter("deptCode");
		
		// 把一个日期类型的字符串解析成Date类型
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 这个Date是java.util下面的Date
		Date date = null;
		try {
			date = format.parse(birthday);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		Emp emp = new Emp(code, name, date, address, phone, password, deptCode);
		int count = empDao.add(emp);
		
		if (count == 1) {
			JSONResult jsonResult = JSONResult.success("添加成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("添加失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
	}

	// 为了转发到/emp/add.jsp，通过后台转发的目的是因为这个页面需要所有的部门的信息，
	// 所以可以在转发到这个页面之前查找出来，到add.jsp页面展示
	private void getAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		ArrayList<Dept> list = deptDao.selectAll();
		
		// 查出所有的部门，放到内存里面
		req.setAttribute("list", list);
		// 转发到add.jsp，因为添加员工的时候需要选择部门
		req.getRequestDispatcher("/web/page/emp/add.jsp").forward(req, resp);
	}



	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// http://localhost:8080/Hotel/emp?mehtod=delete&code=5
		String code = req.getParameter("code");
		
		int count = empDao.delete(code);
		
		// 删除完了之后应该跳转到查找所有的界面，把最新的数据查找展示
		// resp.sendRedirect(req.getContextPath() + "/emp?method=selectAll");
		
		if (count == 1) {
			JSONResult jsonResult = JSONResult.success("删除成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("删除失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String searchName = req.getParameter("name");
		if (searchName == null) {
			searchName = "";
		}
		
		// 调用Dao完成基本的数据库操作
		ArrayList<Emp> list = empDao.selectAll(searchName);
		
		// 把list集合数据放到request内存里面保存
		//req.setAttribute("list", list);
		// 跳转到emp_list.jsp界面展示数据
		//req.getRequestDispatcher("/emp_list.jsp").forward(req, resp);
		
		// 使用LayUI之后需要返回JSON格式的数据
		JSONResult jsonResult = JSONResult.success(list);
		JSONUtil.obj2Json(jsonResult, resp);
		
	}
}
